/*
 * File:    UART2.c
 * Project:	HobbyPilotFirmware (HPF)
 * Author:  Wouter van Verre
 *
 * Created on March 19, 2011, 5:30 PM
 */

/** Includes **/
#include "Header.h"

void initUART2(void)
{
    // Set up the U1MODE register
    U2MODEbits.UARTEN = 0;      // Disable the UART1 module while making changes
    U2MODEbits.USIDL  = 1;      // Discontinue operation while in IDLE mode
    U2MODEbits.WAKE   = 0;      // Don't wake up the mcu when detecting a START bit
    U2MODEbits.LPBACK = 0;      // Don't enable loopback mode
    U2MODEbits.ABAUD  = 0;      // Don't enable auto baud detection mode
    U2MODEbits.PDSEL  = 0;      // 8-bit data, no parity
    U2MODEbits.STSEL  = 0;      // Use one STOP bit

    // Set up the U1STA bits
    U2STAbits.UTXISEL = 1;      // Generate an interrupt when the transmit buffer becomes empty
    U2STAbits.UTXBRK  = 0;      // Operate U1TX normally
    U2STAbits.UTXEN   = 0;      // Disable UART1 transmission
    U2STAbits.URXISEL = 0;      // Generate an interrupt on every character received
    U2STAbits.ADDEN   = 0;      // Disable address detection mode

    // Set up the U1BRG register
    U2BRG = 191;                  // 125,000 @ Fcy = 10MHz

    // Enable the UART1 module
    U2MODEbits.UARTEN = 1;       // Enable the UART1 module
    U2STAbits.UTXEN   = 1;       // Enable UART1 transmission
}


void __attribute__((__interrupt__, __no_auto_psv__)) _U2TXInterrupt (void)
{ 
	EnterInterrupt();   

	IFS1bits.U2TXIF = 0;	// Clear the UART1 module transmission interrupt flag
}

void __attribute__((__interrupt__, __no_auto_psv__)) _U2RXInterrupt (void)
{
	EnterInterrupt();

    IFS1bits.U2RXIF = 0;	// Clear the UART1 module recieve interrupt flag
}	
